package com.ereport.controller.adminController;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.ExecutiveDAO;
import com.ereport.dao.ExpensesInAdminDAO;
import com.ereport.dao.OpportunityDAO;
import com.ereport.entity.AdminExpensesToExec;
import com.ereport.entity.Executives;
import com.ereport.entity.Expenses;
import com.ereport.entity.Opportunity;

@Controller
@RequestMapping("/expensesContrInAdmin")
public class ExpensesControllerInAdmin {
	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		ExecutiveDAO exdao=new ExecutiveDAO();
		List<Executives> exlist=null;
		try {
			exlist=exdao.getExecutives();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		model.setViewName("/jsp/admin/expensesInAdmin.jsp");
		model.addObject("exlist", exlist);
		return model;
	}
	@RequestMapping(value="/save.do", method=RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String eid=req.getParameter("exeName");
		int exeid=0;
		if(eid.length()!=0){
			exeid=Integer.parseInt(eid);
		}
		String month=req.getParameter("month");
		String year=req.getParameter("year");
		String travel=req.getParameter("travel");
		float trvl=0;
		if(!travel.equals("")){
			trvl=Float.parseFloat(travel);
		}
		String hotel=req.getParameter("hotel");
		float htl=0;
		if(!hotel.equals("")){
			htl=Float.parseFloat(hotel);
		}
		String osexp=req.getParameter("osExp");
		float osex=0;
		if(!osexp.equals("")){
			osex=Float.parseFloat(osexp);
		}
		String indAllow=req.getParameter("indAllow");
		float indal=0;
		if(!indAllow.equals("")){
			indal=Float.parseFloat(indAllow);
		}
		String exdate=year+"-"+month;
		ExpensesInAdminDAO opdao=new ExpensesInAdminDAO();
		AdminExpensesToExec aexp=null;
		try {
			aexp=opdao.getExpens(exeid,month,year);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int expnseid=0;
		if(aexp!=null){
			expnseid=aexp.getId();
			try {
				opdao.insert(new AdminExpensesToExec(expnseid,ex,exeid,trvl,htl,osex,indal,exdate));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
		
		try {
			opdao.insert(new AdminExpensesToExec(ex,exeid,trvl,htl,osex,indal,exdate));
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}	
	@RequestMapping(value="/getDetails.do", method=RequestMethod.GET)
	public @ResponseBody
	String getExpenses(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			
			return "/jsp/login.jsp";
		}
		String eid=req.getParameter("exeName");
		String month=req.getParameter("month");
		String year=req.getParameter("year");
		int exeid=0;
		if(!eid.equals("")){
			exeid=Integer.parseInt(eid);
		}
		ExpensesInAdminDAO opdao=new ExpensesInAdminDAO();
		AdminExpensesToExec aexp=new AdminExpensesToExec();
		try {
			aexp=opdao.getExpens(exeid,month,year);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(aexp!=null){
		float travel=aexp.getTravel();
		float hotel=aexp.getHotel();
		float osexp=aexp.getOsExpnse();
		float indvallowns=aexp.getIndivAllowns();
		float total=travel+hotel+osexp+indvallowns;
		StringBuilder sb=new StringBuilder();
		sb.append(travel+"/");
		sb.append(hotel+"/");
		sb.append(osexp+"/");
		sb.append(indvallowns+"/");
		sb.append(total);
		
		return sb.toString();
		}
		return "";
	}
	@RequestMapping("/getViewDetails.do")
	public ModelAndView getView(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		ExpensesInAdminDAO opdao=new ExpensesInAdminDAO();
		List<AdminExpensesToExec> aexpList=new ArrayList<AdminExpensesToExec>();
		try {
			aexpList=opdao.getViewExpens();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(aexpList!=null){
			session.setAttribute("explist", aexpList);
			model.setViewName("redirect:open.do");
			return model;
		}
		model.setViewName("redirect:open.do");
		return model;
	}
}
